<?php
namespace Kuoqi\Api;
require_once(__DIR__.DIRECTORY_SEPARATOR.'email'.DIRECTORY_SEPARATOR.'phpmail'.DIRECTORY_SEPARATOR.'class.phpmailer.php');
use Kuoqi\Config;
/**
 * 这里是phpexcel的基础api的相关控制
 * Class PhpExcel
 * @package Kuoqi\Api
 * User kuoqi
 * Datetime 2022/3/30 10:09
 * Remark
 */
class Email extends \Kuoqi\Api{

    static $host='smtp.163.com';
    static $port="25";
    static $username='';// 必填，开通SMTP服务的邮箱；
    static $password=''; //必填， 以上邮箱对应的密码
    static $altBody='your email do not server for this page'; //当邮件不支持html时备用显示，可以省略
    static $SMTPAuth=true;//开启认证
    static $CharSet='utf-8';//设置邮件的字符编码，这很重要，不然中文乱码
    static $SMTPSecure='ssl';//已重验证中去除
    static $WordWrap='200';// 设置每行字符串的长度

    public $from='kuoqps@163.com';//发送邮箱
    public $fromName='幻冰小站';//发送邮件签名
    public $replyToMail='kuoqps@163.com';//回复地址
    public $replyToName='幻冰小站';//回复地址
    public $nickname = 'dear my friend';//称呼对方的昵称

    /**
     * 初始化
     * Email constructor.
     */
    public function __construct()
    {
        self::$host = Config::get('EMAIL_HOST');
        self::$port = Config::get('EMAIL_PORT');
        self::$username = Config::get('EMAIL_USERNAME');
        self::$password = Config::get('EMAIL_PASSWORD');
    }

    /**
     * 主题字段
     * @var string
     * User kuoqi
     * Datetime 2022/4/20 21:46
     * Remark
     */
    protected $subject = '';
    /**
     * 内容字段
     * @var string
     * User kuoqi
     * Datetime 2022/4/20 21:46
     * Remark
     */
    protected $body = '';
    /**
     * 接收邮箱列表
     * @var array
     * User kuoqi
     * Datetime 2022/4/20 21:47
     * Remark
     */
    protected $email = '';

    /**
     * 发送目标邮箱
     * @param $email
     * User kuoqi
     * Datetime 2022/4/20 21:47
     * Remark
     */
    public function setTo($email){
        $this->email = $email;
    }

    /**
     * 设置邮件主题
     * @param $subject
     * User kuoqi
     * Datetime 2022/4/20 21:48
     * Remark
     */
    public function setSubject($subject){
        $this->subject = $subject;
    }

    /**
     * 设置邮件内容
     * @param $body
     * User kuoqi
     * Datetime 2022/4/20 21:48
     * Remark
     */
    public function setBody($body){
        $this->body = $body;
    }

    /**
     * 发送邮件实际操作
     * @return bool
     * User kuoqi
     * Datetime 2022/4/20 21:54
     * Remark
     */
    public function send(){
        header("content-type:text/html;charset=utf-8");
        $mail=new \PHPMailer();
        //邮件正文内容，提取html文件为其内容
        //$body= $mail->getFile('test/contents.html');
        //$body= eregi_replace("[\]",'',$body);
        $mail->IsSMTP();
        // 必填，SMTP服务器是否需要验证，true为需要，false为不需要
        $mail->SMTPAuth   = self::$SMTPAuth;
        $mail->CharSet    = self::$CharSet;
        $mail->Host       = self::$host;      //必填，设置SMTP服务器
        $mail->Port       = self::$port;      // 设置端口

        $mail->Username   = self::$username;
        $mail->Password   = self::$password;
        $mail->From       = $this->from;    //必填，发件人Email
        $mail->FromName   = $this->fromName;              //必填，发件人昵称或姓名
        $mail->Subject    = $this->subject;       //必填，邮件标题（主题）
        //可选，纯文本形势下用户看到的内容
        $mail->AltBody    = self::$altBody;
        $mail->WordWrap   = self::$WordWrap;
        $mail->MsgHTML($this->body);
        $mail->AddReplyTo($this->replyToMail,$this->replyToName);//回复邮箱地址
        //$mail->AddAttachment("/path/to/file.zip");             // 添加附件,注意路径
        //$mail->AddAttachment(“/path/to/algorithm.jpg”, “new.jpg”); // 添加附件
        //收件人地址。参数一：收信人的邮箱地址，可添加多个。参数二：收件人称呼
        $mail->AddAddress($this->email,$this->nickname);
        $mail->IsHTML(true); // 是否以HTML形式发送，如果不是，请删除此行
        if($mail->Send()) {
            return true;
        } else {
            $this->errmsg = "Mailer错误: " . $mail->ErrorInfo;
            return false;
        }
    }

    /**
     * 快速发送邮件
     * @param $to
     * @param $subject
     * @param $body
     * @return bool
     * User kuoqi
     * Datetime 2022/4/20 21:56
     * Remark
     */
    static public function sendTo($to,$subject,$body){
        $email = new self();
        $email->setTo($to);//配置接收人
        $email->setSubject($subject);//配置邮件主题
        $email->setBody($body);//设置邮件内容
        return $email->send();//邮件发送
    }

}